<?php
/**
 * xhr autocomplete answer class
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: yui.autocomplete.class.php 110 2010-07-23 14:57:13Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * xhr autocomplete answer class
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class yuiAutocomplete
{
    /**
     * @var object instance
     */
    protected static $INSTANCE;
    /**
     * @var object answer
     */
    protected $ANSWER;
    /**
     * instantiate essential objects and make them avail. as subobjects
     *
     * @return yuiAutocomplete
     */
    private function __construct()
    {
        // init result
        $this->ANSWER = new stdclass();
    }
    /**
     * return a instance of object
     *
     * @return yuiAutocomplete instance
     */
    public static function getInstance()
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c;
        }
        return self::$INSTANCE;
    }
    /**
     * return xhr string
     *
     * @return string
     */
    public function __toString()
    {
        $json = new Services_JSON();
        return $json->encode($this->ANSWER);
    }
    /**
     * add a result field and id
     *
     * @param string $field   result fieldname
     * @param string $fieldid result field id (optional)
     *
     * @return bool
     */
    public function addResult($field, $fieldid = null)
    {
        $this->ANSWER->Results[] = array ('field' => $field, 'id' => $fieldid);
        return true;
    }
}